package com.ruoyi.web.controller.member;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysNotice;
import com.ruoyi.video.domain.BusDownloadInfo;
import com.ruoyi.video.domain.BusMemberInfo;
import com.ruoyi.video.domain.dto.PromDetailDTO;
import com.ruoyi.video.domain.dto.PromListDetailDTO;
import com.ruoyi.video.service.IDownloadInfoService;
import com.ruoyi.video.service.IMemberInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员 信息操作处理
 * 
 * @author ruoyi
 */
@Api("会员信息管理")
@RestController
@RequestMapping("/bus/member")
public class BusMemberController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(BusMemberController.class);

    @Autowired
    private IMemberInfoService memberInfoService;

    @Autowired
    private IDownloadInfoService downloadInfoService;

    /**
     * 获取会员列表
     */
    @ApiOperation("获取会员列表")
    @PreAuthorize("@ss.hasPermi('bus:member:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusMemberInfo member)
    {
        startPage();
        List<BusMemberInfo> memberInfoList = memberInfoService.selectMemberList(member);
        for (BusMemberInfo memberInfo : memberInfoList) {
            int memberNum = 0;
            int downloadNum = 0;
            List<PromDetailDTO> promotionList = new ArrayList<>();
            List<BusMemberInfo> sonMemberInfoList = memberInfoService.selectMemberListByPId(memberInfo.getMemberId()); // 子会员信息
            if (StringUtils.isNotEmpty(sonMemberInfoList)) {
                memberNum = sonMemberInfoList.size();
                promotionList = memberInfoService.selectPromStatisticsByPMemberId(memberInfo.getMemberId(), memberInfo.getRebatePercent());  // 推广信息
            }
            List<BusDownloadInfo> downloadList = downloadInfoService.selectDownloadListByPromCode(memberInfo.getPromCode());
            if (StringUtils.isNotEmpty(downloadList)) {
                downloadNum = downloadList.size();
            }
            memberInfo.setPromotionStatistics(new PromListDetailDTO(memberNum, downloadNum, promotionList));
        }
        return getDataTable(memberInfoList);
    }

    /**
     * 根据会员编号获取详细信息
     */
    @ApiOperation("获取会员详情")
    @PreAuthorize("@ss.hasPermi('bus:member:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable String memberId) {
        BusMemberInfo memberInfo = memberInfoService.selectMemberById(memberId);
        if (memberInfo == null || StringUtils.isEmpty(memberInfo.getMemberId())) {
            return AjaxResult.error("用户信息不存在");
        }
        BusMemberInfo pMemberInfo = memberInfoService.selectMemberById(memberInfo.getpMemberId());
        memberInfo.setpMemberInfo(pMemberInfo);
        int memberNum = 0;
        int downloadNum = 0;
        int rebatePercent = memberInfo.getRebatePercent();
        List<PromDetailDTO> promotionList = new ArrayList<>();
        List<BusMemberInfo> memberInfoList = memberInfoService.selectMemberListByPId(memberId); // 子会员信息
        if (StringUtils.isNotEmpty(memberInfoList)) {
            memberNum = memberInfoList.size();
            promotionList = memberInfoService.selectPromStatisticsByPMemberId(memberId, rebatePercent);  // 推广信息
        }
        List<BusDownloadInfo> downloadList = downloadInfoService.selectDownloadListByPromCode(memberInfo.getPromCode());
        if (StringUtils.isNotEmpty(downloadList)) {
            downloadNum = downloadList.size();
        }
        memberInfo.setPromotionStatistics(new PromListDetailDTO(memberNum, downloadNum, promotionList));
        return AjaxResult.success(memberInfo);
    }

    /**
     * 修改会员信息
     */
    @ApiOperation("修改会员信息")
    @PreAuthorize("@ss.hasPermi('bus:member:edit')")
    @Log(title = "会员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody BusMemberInfo memberInfo)
    {
        memberInfo.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(memberInfoService.updateMember(memberInfo));
    }


    /**
     * 删除会员
     */
    @ApiOperation("删除会员信息")
    @PreAuthorize("@ss.hasPermi('bus:member:remove')")
    @Log(title = "会员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable String[] memberIds) {
        return toAjax(memberInfoService.deleteMemberByIds(memberIds));
    }
}
